using System.Collections.Generic;
using System.Linq;
using Msic;
using Sirenix.OdinInspector;
using Sirenix.Serialization;
using UnityEngine;

namespace cmd
{
    // [CreateAssetMenu(fileName = "LevelData", menuName = "cmd/LevelData")]
    /// <summary>关卡数据模型，通过GetPickableItemsInLayer(int layer)方法来得到对应层的物品(注：每次调用时都会分配一次随机生成的物品)</summary>
    [AssetList]
    public class LevelDataModelSO : SerializedScriptableObject
    {
        [field: TitleGroup("基础信息")]
        [field: SerializeField]
        [field: LabelText("关卡编号")]
        public uint Id { get; protected set; }

        [field: TitleGroup("基础信息")]
        [field: SerializeField]
        [field: LabelText("关卡名称")]
        public string LevelName { get; protected set; }

        [TitleGroup("基础信息")]
        [ShowInInspector, ReadOnly]
        [LabelText("关卡总层数")]
        public int LayerCount => LayerDatas.Count;

        [field: TitleGroup("场景配置")]
        [field: OdinSerialize]
        [field: LabelText("场景数据列表"), PropertyTooltip("每一个子集合都代表一层的随机列表")]
        [field: ListDrawerSettings(
            ShowIndexLabels = true,
            CustomAddFunction = nameof(CustomLayerCongigurationAddFunction)
        )]
        // [field: InlineEditor]
        // [field: AssetList]
        public List<RandomLayerGenerateConfiguration> LayerDatas { get; protected set; } = new();

        private RandomLayerGenerateConfiguration CustomLayerCongigurationAddFunction() =>
            new()
            {
                ConfigMethod = RandomLayerGenerateConfiguration
                    .LayerGenerateConfigurationMethod
                    .UseSO
            };

        [field: TitleGroup("物品生成")]
        [field: SerializeField]
        [field: LabelText("随机物品生成配置")]
        [field: PropertyTooltip("总的配置，关卡各场景默认跟随此配置")]
        public RandomItemGenerateDataSO RandomItemGenerateDataSO { get; protected set; }

        #region 公共方法
        /// <summary>
        /// 获取关卡指定层所有生成的物品(每次生成时随机)
        /// </summary>
        /// <param name="layer">层数，最小为0，最大为LayerCount - 1</param>
        /// <returns>(抽选得到的层, 生成的可拾取物品集合)，如果层数不合法返回(null, null)</returns>
        // [FoldoutGroup("测试方法", Expanded = false)]
        [TitleGroup("测试方法")]
        [Button(ButtonSizes.Medium)]
        public (
            LevelLayerDataSO layerDataSO,
            List<PickableItemDataDTO> pickableItems
        ) GetPickableItemsInLayer(int layer)
        {
            if (!LayerDatas.TryGet(layer, out var randomLayerData))
            {
                Debug.LogWarning($"关卡 {Id} 不存在第 {layer} 层");
                return (null, null);
            }

            LevelLayerDataSO pickedLayerSO = randomLayerData.Data.GetRandomLayer();
            return (
                pickedLayerSO,
                pickedLayerSO.LevelLayerData.GetAllPickableItemDTOs(
                    RandomItemGenerateDataSO != null
                        ? RandomItemGenerateDataSO.RandomItemGenerateData
                        : null
                )
            );
        }

        /// <summary>
        /// 获取关卡所有层生成的所有物品(每次生成时随机)
        /// </summary>
        /// <returns>所有层生成的可拾取物品集合的集合</returns>
        public IEnumerable<(
            LevelLayerDataSO layerDataSO,
            List<PickableItemDataDTO> pickableItems
        )> GetPickableItemsInAllLayers()
        {
            for (int i = 0; i < LayerCount; i++)
            {
                yield return GetPickableItemsInLayer(i);
            }
        }

        #endregion
        #region Unity生命周期
        [Button(ButtonSizes.Medium), PropertySpace(10)]
        private void SetLevelNameAndId()
        {
            // 提取name开头的数字作为Id
            LevelName = name.TrimStart('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
            if (uint.TryParse(name[..^LevelName.Length], out uint id))
            {
                Id = id;
            }

            LevelDataManager.CheckDuplicateLevelIds(false);
        }

        // void Awake()
        // {
        //     Id = ++LevelDataManager.MaxLevelDataId;
        // }
        #endregion
    }
}
